###############################
###############################
###Figure 1.2: CAFSI/CP Maps###
###############################
###############################
library(gganimate)
library(ggplot2)
library(maps)
library(ggthemes)
library(doBy)
#Set WD
setwd("~/OneDrive - Indiana University/FromGoogle/SouthSudanProject/Code_12_20_24/")
#Import data
ALLNGO <- read.csv("ingodatmap_9_20_24.csv")
summary(ALLNGO)

#############################
###Clean and Organize Data###
#############################
#Rename the focus area variable
ALLNGO$`Focus area` <- ALLNGO$Focus.area
######South Sudan only
##Define countries to be plotted
african.countries <- c(
  "South Sudan"
)

###Remove NAs
ALLNGO <- ALLNGO[!is.na(ALLNGO$Food.Security),]
ALLNGO <- ALLNGO[!is.na(ALLNGO$Climate.Adaptation),]
#Keep only South Sudan projects for now
ALLNGO.south <- subset(ALLNGO, COW==626)
#Remove projects on Sudan's side
ALLNGO.south$ind_rem <- ifelse( ((ALLNGO.south$Latitude>9.3&ALLNGO.south$Latitude<13.72)& (ALLNGO.south$Longitude>28 & ALLNGO.south$Longitude<30)),1,0)
table(ALLNGO.south$ind_rem )
ALLNGO.south <- subset(ALLNGO.south, ind_rem==0)

#Juba coordinates
coordinate_juba <- data.frame(t(c(1,4.85165,31.58247)))
names(coordinate_juba) <- c("city", "lat","long")
coordinate_juba$city <- ifelse(coordinate_juba$city==1, "Juba",coordinate_juba$city)
summary(coordinate_juba)

##############
###Plot map###
##############
#Begin with a map of the African states
world <- ggplot() +
  borders("world", regions=african.countries, colour = "gray85", fill = "gray80") +
  theme_map() 

#Color scale
cc <- scales::seq_gradient_pal("red", "blue", "Lab")(seq(0,1,length.out=4))

##Create the main map
map.all.col1 <- world +
  geom_point(aes(x = Longitude, y = Latitude, color = `Focus area`, size=count), size=1.5,
             data = ALLNGO.south) +
  scale_colour_manual(values=cc)+
  theme(legend.position = "bottom")+
  geom_text(data = coordinate_juba, aes(label = city,x = long, y = lat), size=4, vjust = -1,fontface = "bold")

##Export map
jpeg("Figure12.jpeg", width = 6, height = 6, units = 'in', res = 500)
map.all.col1
dev.off() 


